package com.javatest.ajax.app3.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.javatest.ajax.app3.bean.Department;
import com.javatest.ajax.app3.bean.Employee;
import com.javatest.ajax.app3.bean.Location;
import com.javatest.ajax.app3.dao.DaoFactory;
import com.javatest.ajax.app3.dao.IDepartmentDao;
import com.javatest.ajax.app3.dao.IEmployeeDao;
import com.javatest.ajax.app3.dao.ILocationDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2018-09-04 8:29
 */
public class EmployeeServlet extends HttpServlet {
    private static final long serialVersionUID = 4665089648761234181L;
    private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeServlet.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methodName = req.getParameter("method");
        Method method = null;
        try {
            method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            if (method != null) {
                method.invoke(this, req, resp);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            LOGGER.error("EmployeeServlet invoke error!", e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    private void listLocations(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ILocationDao locationDao = DaoFactory.getLocationDao();
        List<Location> locations = locationDao.getAllLocations();
        req.setAttribute("locations", locations);
        req.getRequestDispatcher("/app3/pages/employees.jsp").forward(req, resp);
    }

    private void listDepartment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InterruptedException {
        String parmId = req.getParameter("locationId");
        if (parmId != null) {
            int id = Integer.parseInt(parmId);
            IDepartmentDao departmentDao = DaoFactory.getDepartmentDao();
            List<Department> departments = departmentDao.getDepartments(id);
            req.setAttribute("departments", departments);
            PrintWriter writer = resp.getWriter();
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(departments);
            LOGGER.info("department:" + json);
            TimeUnit.SECONDS.sleep(1);
            writer.print(json);
        }
    }

    private void listEmployees(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InterruptedException {
        String parmId = req.getParameter("departmentId");
        if (parmId != null) {
            int id = Integer.parseInt(parmId);
            IEmployeeDao employeeDao = DaoFactory.getEmployeeDao();
            List<Employee> employees = employeeDao.getEmployees(id);
            req.setAttribute("employees", employees);
            PrintWriter writer = resp.getWriter();
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(employees);
            LOGGER.info("employees:" + json);
            TimeUnit.SECONDS.sleep(1);
            writer.print(json);
        }
    }

    private void getEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InterruptedException {
        String parmId = req.getParameter("employeeId");
        if (parmId != null) {
            int id = Integer.parseInt(parmId);
            IEmployeeDao employeeDao = DaoFactory.getEmployeeDao();
            Employee employee = employeeDao.getEmployee(id);
            req.setAttribute("employee", employee);
            PrintWriter writer = resp.getWriter();
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(employee);
            LOGGER.info("employee:" + json);
            TimeUnit.SECONDS.sleep(1);
            writer.print(json);
        }
    }
}
